﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Reflection;
using System.Xml;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.IO;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Site {
    public partial class SmtpConfig : PageSupport {
        IUserBiz userBiz = BizFactory.Get<IUserBiz>();
        XmlDocument doc = new XmlDocument();
        String cfgFilePath = ClientHelper.RootPath + "web.config";
        protected void Page_Load(object sender, EventArgs e) {
            doc.Load(cfgFilePath);
            if (!Page.IsPostBack) {
                InitForm();
                FillForm();
            }
        }
        void InitForm() {
        }
        void FillForm() {
            this.txt_SmtpFrom.Text = ClientBiz.GetAppItem(ref doc,AppSettingsKey.SmtpFrom);
            this.txt_SmtpPassword.Text = ClientBiz.GetAppItem(ref doc,AppSettingsKey.SmtpPwd);
            this.txt_SmtpServer.Text = ClientBiz.GetAppItem(ref doc,AppSettingsKey.SmtpServer);
            this.txt_SmtpUser.Text = ClientBiz.GetAppItem(ref doc,AppSettingsKey.SmtpUser);
            this.txt_SmtpPort.Text = ClientBiz.GetAppItem(ref doc, AppSettingsKey.SmtpPort,SiteConfig.SMTP_PORT_DEFAULT.ToString());
            this.cbx_SSL.Checked = SiteConfig.Smtp.SmtpSSL;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            try {
                File.Copy(cfgFilePath, cfgFilePath + ".bak", true);
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "备份配置文件时错误");
                return;
            }
            try {
                ClientBiz.SetAppItem(ref doc,AppSettingsKey.SmtpFrom, this.txt_SmtpFrom.Text);
                ClientBiz.SetAppItem(ref doc,AppSettingsKey.SmtpPwd, this.txt_SmtpPassword.Text);
                ClientBiz.SetAppItem(ref doc,AppSettingsKey.SmtpServer, this.txt_SmtpServer.Text);
                ClientBiz.SetAppItem(ref doc,AppSettingsKey.SmtpUser, this.txt_SmtpUser.Text);
                ClientBiz.SetAppIntItem(ref doc, AppSettingsKey.SmtpPort, this.txt_SmtpPort.Text,SiteConfig.SMTP_PORT_DEFAULT);
                doc.Save(cfgFilePath);
                BizFactory.Get<ILogBiz>().Info("进行了邮件服务设置操作", this.Actor, Request);
                SiteInfo.SendSucceed(Page, "");
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page,TxtDef.ERROR,"保存配置项时出现错误，保存失败");
            }
        }
    }
}
